<template>
    <div id="search">
        <baseinfo></baseinfo>
        <div class="title">
            <i class="el-icon-star-on"></i>
            <span>搜索结果</span>
            <i class="el-icon-star-on"></i>
        </div>
        <div class="showpro">
        <!-- image="https://img13.360buyimg.com/jdcms/s300x300_jfs/t1/178661/28/3905/138182/609d39a7Eb1a7b045/c6f860d88ffe3ffd.jpg.webp"
        name="百草味 冰雪蛋糕540g/箱
                网红饼干糕点点心整箱早餐手撕小面包麻薯夹心糯米糍"
        price="99.9"
        sellername="百草味官方旗舰店"
        id="3" -->
        <productBrief
        v-for="item in searchResult"
        :key="item.index"
        :image="item.image"
        :name="item.name"
        :price="item.price"
        :sellername="item.sellerUsername"
        :id="item.pid"
        ></productBrief>
      </div>
      <div id="showneed">
        <need 
          v-for="item in searchResult2" 
          :key="item.index"
          :image="item.image"
          :username="item.username"
          :needTime="item.time"
          :description="item.description"
        ></need>
      </div>
    </div>
</template>

<script>
import baseinfo from '../components/baseinfo'
import productBrief from '../components/productBrief'
import need from '../components/need'
import {request} from '../network/request.js'

export default {
    components: {
        baseinfo, productBrief, need
    },
    data() {
    return{
      searchResult: [],
      searchResult2: [],
      keyword:this.$route.query.keyword,
    }
  },
  watch:{
    $route(){
        this.keyword = this.$route.query.keyword
        this.search()
      }
  },
  methods: {
    search(){
      if(this.keyword.length > 0){
        console.log(this.keyword)
        request({
          method: 'get',
          url: '/search',
          params: {
            keyword: this.keyword
          }
        }).then(res => {
          console.log(res)
          this.searchResult=res.data.products;
          this.searchResult2=res.data.needs;
          if(res.data.res == 1){
              this.$message({
                message: "暂无搜索内容，换个关键词试试吧！",
                type: "success",
            });
          }
        })
      }else{
        this.$message({
            message: "请输入搜索内容！",
            type: "success",
        });
      }
    },
  },
  created(){
      if(this.keyword.length > 0){
        console.log(this.keyword)
        request({
          method: 'get',
          url: '/search',
          params: {
            keyword: this.keyword
          }
        }).then(res => {
          console.log(res)
          this.searchResult=res.data.products;
           this.searchResult2=res.data.needs;

          if(res.data.res == 1){
              this.$message({
                message: "暂无搜索内容，换个关键词试试吧！",
                type: "success",
            });
          }
        })
      }else{
        this.$message({
            message: "请输入搜索内容！",
            type: "success",
        });
      }
    }
}
</script>

<style scoped>

.title {
    text-align: center;
    font-size: 28px;
    font-weight: bold;
    padding-top: 25px;
    padding-bottom: 20px;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    width: 200px;
}

.title i {
    padding: 5px;
}

.showpro {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
</style>

